from django.shortcuts import render
from django.http import HttpResponseRedirect,HttpResponse
from .models import *

def add_room(request):
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        try:
            to = Tower.objects.get(tower_name=info['add_tower_name'][0])
            fo = Floor.objects.get(floor_num=info['add_floor_num'][0])
            ro = Room.objects.get(room_num=info['add_room_num'][0])
        except Tower.DoesNotExist:
            if info['add_floor_num'][0] == '' and info['add_room_num'][0] == '':
                Tower.objects.create(tower_name=info['add_tower_name'][0])
                return HttpResponse('楼号添加成功')

            elif info['add_floor_num'][0] != '' and info['add_room_num'][0] == '':
                try:
                    Tower.objects.get(tower_name=info['add_tower_name'][0])
                except Tower.DoesNotExist:
                    return HttpResponse('此楼不存在')
                else:
                    Floor.objects.create(floor_name=info['add_tower_name'][0],
                                         tower=info['add_tower_name'][0] )

                    return HttpResponse('楼层添加成功')
            elif info['add_floor_num'][0] != '' and info['add_room_num'][0] != '':
                try:
                    Tower.objects.get(tower_name=info['add_tower_name'][0])
                except Tower.DoesNotExist:
                    return HttpResponse('此楼不存在')
                else:
                    try:
                        Floor.objects.get(floor_num=info['add_floor_num'][0])
                    except Floor.DoesNotExist:
                        return HttpResponse('此楼层不存在')
                    else:
                        try:
                            Room.objects.get(room_num=info['add_room_num'][0])
                        except Room.DoesNotExist:
                            Room.objects.get(room_num=info['add_room_num'][0],
                                             floor = info['add_floor_num'][0])
                            return HttpResponse('房间添加成功')
    elif request.method == 'GET':
        return HttpResponse('不接受GET')









